package splug.cjk;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.ArrayList;

import splug.cjk.packet.FriendPacket;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class ReportTab1 extends Activity {

	private String id;
	private EditText text_id;
	private EditText text_reason;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.report_tab1);
		setTitle("신고하기");

		//나의 아이디 받아오기
		Bundle b = this.getIntent().getExtras();
		id = b.getString("ID");

		//컴포넌트들의 ID를 받아온다
		text_id = (EditText) findViewById(R.id.text_report_id);
		text_reason = (EditText) findViewById(R.id.text_report_reason);
		Button btn_report = (Button) findViewById(R.id.btn_report_ok);
		Button btn_back = (Button) findViewById(R.id.btn_report_cancel);

		//신고하기 버튼 이벤트 추가
		btn_report.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String you = text_id.getText().toString();
				String reason = text_reason.getText().toString();
				
				if(you.equals("")){
					Toast.makeText(ReportTab1.this, "아이디를 입력해주세요!", Toast.LENGTH_SHORT).show();
				}
				else if(you.length() > 20){
					Toast.makeText(ReportTab1.this, "해당하는 회원이 없습니다", Toast.LENGTH_SHORT).show();
				}
				else if(reason.length() > 200){
					Toast.makeText(ReportTab1.this, "내용은 200자까지 작성 가능합니다", Toast.LENGTH_SHORT).show();
				}
				else if(reason.equals("")){
					Toast.makeText(ReportTab1.this, "내용을 입력해주세요!", Toast.LENGTH_SHORT).show();
				}
				else{
					//신고하기 요청 번호는 22번
					try {
						Socket socket = new Socket(LetsGoActivity.server_ip, 2007);

						InputStream is = socket.getInputStream();
						OutputStream os = socket.getOutputStream();

						OutputStreamWriter osw = new OutputStreamWriter(os);
						InputStreamReader isr = new InputStreamReader(is);

						osw.write(22);
						osw.flush();

						int ack = isr.read(); 

						if(ack == '1'){
							//FriendPacket을 재활용 한다
							ArrayList<String> text = new ArrayList<String>();
							text.add(reason);
							FriendPacket packet = new FriendPacket(text, id, you);
							ObjectOutputStream output = new ObjectOutputStream(os);
							output.writeObject(packet);

							ack = isr.read();

							if(ack == '1'){
								Toast.makeText(ReportTab1.this, "신고 완료!", Toast.LENGTH_SHORT).show();
								finish();
							}
							else if(ack == '0'){
								Toast.makeText(ReportTab1.this, "해당하는 회원을 찾을 수 없습니다", Toast.LENGTH_SHORT).show();
							}
							else if(ack == '2'){
								Toast.makeText(ReportTab1.this, "이미 신고를 하였습니다", Toast.LENGTH_SHORT).show();
							}
						}
						else{
							Toast.makeText(ReportTab1.this, "네트워크 연결상대가 좋지 않습니다", Toast.LENGTH_SHORT).show();
							finish();
						}


					} catch (Exception e) {
						Toast.makeText(ReportTab1.this, "네트워크 연결상대가 좋지 않습니다", Toast.LENGTH_SHORT).show();
						finish();
					}
				}
			}
		});		

		//뒤로가기 버튼 이벤트 추가
		btn_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});		
	}

}
